$(function() {
    var scroll = new Scroll(), slider = new Slider(), table = new Table();
    slider.init();
    //scroll the window when navigation was clicked.
    $(".nav a").click(function() {
        scroll.to($(this).attr("href"));
        return false;
    });

    $(window).scroll(function(e) {
        onWindowScroll();
    });

    //goto top when handSymbol was clicked
    $("#handSymbol").click(function() {
        scroll.to("#top");
    });

    //shows the full text of 3 sections
    $(".btncon").click(function() {
        $(this).parent().hide().next().slideDown();
        return false;
    });

    function Scroll() {
        var timer = null;
        this.to = function(id) {
            var start = $(window).scrollTop();
            var target = $(id).offset().top;
            var direct = target > start ? 1 : -1;
            var step = Math.max(Math.abs((target - start) / 100), 10) * direct;
            timer && clearInterval(timer);
            timer = setInterval(function() {
                var cur = $(window).scrollTop();
                if ((cur - target) * direct >= 0)
                    clearInterval(timer);
                else
                    scrollTo(0, start += step);
            }, 1);
        };
    }

    function Slider() {
        var timer, v = this, intervalTime = 5000;
        this.next = function(direction) {
            clearInterval(timer);
            direction = direction || 1;
            var lis = $(".ani li");
            var temp = lis.eq(0).attr("class");
            for (var i = 0, j = 0; j < lis.length; j++, i += direction) {
                var newClass = Math.abs(i) !== lis.length - 1 ? lis.eq(i + direction).attr("class") : temp;
                lis.eq(i).attr("class", newClass);
            }
            timer = setTimeout(v.next, intervalTime);
        };
        this.init = function() {
            $("#slider .prev").click(function() {
                slider.next(-1);
                return false;
            });
            $("#slider .next").click(function() {
                slider.next();
                return false;
            });
            timer = setTimeout(v.next, intervalTime);
        };
    }
    ;

    function onWindowScroll() {
        //show the top navigation when the scrollTop of window great than 70
        if ($(window).scrollTop() > 70)
            $(".fix_menu").show();
        else
            $(".fix_menu").hide();
        var sections = {"#top": {"left": 55, "top": 0}, '#about': {"left": 870, "top": 80},
            '#competition41': {"left": 20, "top": 40}, '#competition40': {"left": 860, "top": 40}};
        var windowScrollTop = $(window).scrollTop();
        for (var id in sections) {
            var section = $(id), sectionTop = section.offset().top, sectionHeight = section.height();

        }
    }

    function Table() {
        var datas = new Array();
        $("#result > .resultDiv > div").each(function() {
            if($(this).find("h2").length === 0)return;
            var match = $(this).find("h2").html();
            $(this).find("ul > li").each(function() {
                if($(this).find("div").length > 0){
                    var module = $(this).find("div").html();
                    $(this).find("ul > li").each(function() {
                        var li = $(this);
                        var country = li.attr("data-country"), level = $(this).attr("title"), team = li.html();
                        if(country){
                            datas.push({match: match, module: module, country: country, level: level, team: team});
                        }
                    });
                }
            });
        });
        $("#result > .resultDiv > div").remove();
        var table = $("#resultTable").find("tbody").empty();
        for (var i in datas) {
            var data = datas[i];
            var tr = $("<tr></tr>");
            tr.append("<td>" + data.match + "</td>")
                    .append("<td>" + data.module + "</td>")
                    .append("<td>" + data.country + "</td>")
                    .append("<td>" + data.level + "</td>")
                    .append("<td>" + data.team + "</td>");
            table.append(tr);
        }
        
        function search(){
            var keys = $("#ipt").val(), lowerCase = !$("#caseIpt").get(0).checked;
            if(lowerCase) keys = keys.toLowerCase();
            keys = keys.split("+");
            var num = 0;
            $("#resultTable tbody tr").hide().each(function(i){
                var data = datas[i], founded = true;
                for(var j in keys){
                    var key = keys[j];
                    var haskey = false;
                    for(var k in data){
                        if((lowerCase ? data[k].toLowerCase() : data[k]).indexOf(key) >= 0){
                            haskey = true;
                            break;
                        }
                    }
                    founded = founded & haskey;
                }
                if(founded){
                    $(this).show();
                    num++;
                }
            });
            if(num === 0){
                $("#noresultDiv").show();
                $(".keyspan").html($("#ipt").val());
            }else{
                $("#noresultDiv").hide();
            }
        }
		
        $("#resultTable tbody tr").each(function (){
           $(this).find("td").not(function (index){return index === 0;}).click(function (e){
               var val = this.innerHTML.replace(/^[a-z0-9A-Z]+\s+\-\s+/g, ""), iptVal = $("#ipt").val();
			    if(iptVal != "" && iptVal.indexOf(val) >= 0) return false;
			    if(e.shiftKey && iptVal != "") val = iptVal + "+" + val;
               $("#ipt").val(val);
               search();
           });
        });
        
        $("#ipt").keypress(function (){
           setTimeout(search, 1);
        });
        $("#caseIpt").click(function (){
            setTimeout(search, 1);
        });
    }
});

